package net.findkrypton.event;

import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import net.findkrypton.po.LoadSave;
import net.jumpengine.M;
import net.jumpengine.Manager;
import net.jumpengine.bean.Base;
import net.jumpengine.bean.BoxBase;
import net.jumpengine.bean.ButtonBase;
import net.jumpengine.bean.ImageBase;
import net.jumpengine.bean.TextRect;
import net.jumpengine.bean.TimeBase;
import net.jumpengine.enume.GameState;
import net.jumpengine.inter.IAction;
import net.jumpengine.inter.IStateEvent;
import net.jumpengine.inter.ITimeHandle;
import net.jumpengine.manager.ImageManager;
import net.jumpengine.po.Border;

public class MenuStateEvent implements IStateEvent {
	private ImageBase back;
	private boolean load;
	private TextRect tip,confirm;
	private TimeBase time,quite;
	private boolean moreClick=false;

	@Override
	public void setBackImage(Image image) {
		if(back==null) {
			back=new ImageBase(0, 0, image);
			M.i().add(back);
		}else {
			back.setImage(image);
		}
	}

	@Override
	public boolean isLoaded() {
		return load;
	}

	@Override
	public void load() {
		ImageManager im = M.i();
		
		Border border=new Border(2, 2, 2, 2, 2);
		int x=(M.w()-320)/2,y=(M.h()-470)/2;
		BoxBase box=new BoxBase(x, y, 320, 470, border, Color.web("#005A85"));
		im.add(box);
		
		Font font=new Font("黑体", 20);
		ButtonBase btu=new ButtonBase(x+50, y+40, 210, 50, "保存游戏");
		btu.setFont(font);
		btu.setAction(new IAction() {
			@Override
			public void handle(int x, int y, MouseButton button, Base base) {
				RunningStateEvent event = (RunningStateEvent) M.e().getStateEvent(GameState.running);
				if(event.getArray().isLoser()) {
					tip.setText("已失败,无法保存");
					tip.setVisible(true);
					time.setAble(true);
					return;
				}
				
				LoadSave.save();
				tip.setText("游戏已保存!");
				tip.setVisible(true);
				time.setAble(true);
			}
		});
		im.add(btu);
		
		tip=new TextRect(x+50,y+95,210,30,"游戏已保存!");
		tip.setFont(font);
		tip.setBackColor(Color.rgb(118,82,15));
		tip.setColor(Color.WHITE);
		
		time=new TimeBase(3000, tip, new ITimeHandle() {
			@Override
			public void timeHandle(TimeBase time, Base source) {
				source.setVisible(false);
			}
		});
		tip.setVisible(false);
		time.setAble(false);
		im.add(tip);
		im.addUpdate(time);
		
		btu=new ButtonBase(x+50, y+130, 210, 50, "读取游戏");
		btu.setFont(font);
		btu.setAction(new IAction() {
			@Override
			public void handle(int x, int y, MouseButton button, Base base) {
				Manager.setState(GameState.load,true);
			}
		});
		im.add(btu);
		
		btu=new ButtonBase(x+50, y+220, 210, 50, "返回主菜单");
		btu.setAction(new IAction() {
			@Override
			public void handle(int x, int y, MouseButton button, Base base) {
				Manager.setState(GameState.start);
			}
		});
		btu.setFont(font);
		im.add(btu);
		
		btu=new ButtonBase(x+50, y+310, 210, 50, "退出游戏");
		btu.setAction(new IAction() {
			@Override
			public void handle(int x, int y, MouseButton button, Base base) {
				if(moreClick) {
					System.exit(0);
				}else {
					confirm.setVisible(true);
					quite.setAble(true);
					moreClick=true;
				}
			}
		});
		btu.setFont(font);
		im.add(btu);
		
		confirm=new TextRect(x+50, y+365, 210, 30, "再次点击退出游戏!");
		confirm.setFont(font);
		confirm.setBackColor(Color.rgb(118,82,15));
		confirm.setColor(Color.WHITE);
		confirm.setVisible(false);
		im.add(confirm);
		
		quite=new TimeBase(3000, confirm, new ITimeHandle() {
			@Override
			public void timeHandle(TimeBase time, Base source) {
				source.setVisible(false);
				moreClick=false;
			}
		});
		im.addUpdate(quite);
		
		btu=new ButtonBase(x+50, y+400, 210, 50, "返回游戏");
		btu.setFont(font);
		btu.setAction(new IAction() {
			@Override
			public void handle(int x, int y, MouseButton button, Base base) {
				Manager.setState(GameState.running);
			}
		});
		im.add(btu);
		
		load=false;
	}

	@Override
	public void show() { }

	@Override
	public void pause() { }

	@Override
	public void end() { }

}
